package com.ttedu.action.manage.types;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.ttedu.base.BaseAction;
import com.ttedu.database.model.types;
import com.ttedu.database.service.TypesService;
import com.ttedu.util.KeyUtil;
import com.ttedu.util.Pager;
import com.ttedu.util.Validator;

public class TypesAction extends BaseAction{
	private types type=new types();
	private static  int pageSize; //分页显示的条数
	private Pager pager = new Pager();//分页参数
	@Autowired
	private TypesService typesservice;
	/**
	 * 查询页
	 * */
	@Action(value="/cas/types_index",results={ @Result(name="success",location="/WEB-INF/html/manage/types/index.jsp")})
	public String types_index(){
		String offset_=request.getParameter("pager.offset");
		int offset=Pager.getoffset(offset_);
		if(pageSize<=0){
            pageSize=20;
        }
		//查询条件
		Map paramMap = new HashMap();
		paramMap.put("offset", offset);
		paramMap.put("length", pageSize);
		paramMap.put("name", type.getName());
		List<types> listtypes=typesservice.selectTypesAll(paramMap);
		int total=typesservice.selectTypesCount(paramMap);
		request.setAttribute("listtypes", listtypes);
		request.setAttribute("total", total);
		return "success";
	}
	/**
	 * 跳转到添加页面
	 * */
	@Action(value="/cas/types_add",results={ @Result(name="success",location="/WEB-INF/html/manage/types/add.jsp")})
	public String types_add(){
		request.setAttribute("dtypesList", KeyUtil.dtypesList());
		return "success";
	}
	/**
	 * 删除
	 * */
	@Action(value="/cas/types_delete",results={ @Result(name="success",type="redirect",location="/cas/types_index.action")})
	public String types_delete(){
		String id=request.getParameter("id");
		typesservice.deleteByPrimaryKey(Integer.valueOf(id));
		return "success";
	}
	/**
	 * 跳转到编辑页面
	 * */
	@Action(value="/cas/types_edit",results={ @Result(name="success",location="/WEB-INF/html/manage/types/edit.jsp")})
	public String sp_edit(){
		String id=request.getParameter("id");
		type =typesservice.selectByPrimaryKey(Integer.valueOf(id));
		request.setAttribute("dtypesList", KeyUtil.dtypesList());
		return "success";
	}
	@Action(value="/cas/types_updateorsave",results={ @Result(name="success",type="redirect",location="/cas/types_index.action")})
	public String user_updateorsave(){
		//更新
		if(Validator.isNotNull(type.getId())){
			types typesclent =typesservice.selectByPrimaryKey(type.getId());
			typesclent.setName(type.getName());//名称
			typesclent.setTypecode(type.getTypecode());//编号
			typesclent.setDtype(type.getDtype());//内置类型
			typesclent.setAuto(type.getAuto());//自动判分
			typesclent.setMixed(type.getMixed());//复合题
			typesclent.setUpdatedAt(new Date());
			typesservice.updateByPrimaryKey(typesclent);
		}
		//保存
		else{
			type.setCreatedAt(new Date());
			type.setUpdatedAt(new Date());
			typesservice.insert(type);
		}
		return "success";
	}
	
	public types getType() {
		return type;
	}

	public void setType(types type) {
		this.type = type;
	}

	public static int getPageSize() {
		return pageSize;
	}

	public static void setPageSize(int pageSize) {
		TypesAction.pageSize = pageSize;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}
	
}
